package com.hh.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hh.common.ErrorCode;
import com.hh.constant.RedisLuaScriptConstant;
import com.hh.entity.domain.User;
import com.hh.entity.domain.UserCollection;
import com.hh.entity.dto.collect.DoCollectRequest;
import com.hh.entity.enums.LuaStatusEnum;
import com.hh.exception.BusinessException;
import com.hh.mapper.UserCollectionMapper;
import com.hh.service.UserCollectionService;
import com.hh.service.UserService;
import com.hh.utils.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

import static com.hh.constant.CollectConstant.COLLECT_KEY_PREFIX;

/**
 * @author hanhui
 * @description 针对表【user_collection】的数据库操作Service实现
 * @createDate 2025-06-01 18:29:16
 */
@Slf4j
@Service
public class UserCollectionServiceImpl extends ServiceImpl<UserCollectionMapper, UserCollection>
        implements UserCollectionService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private UserService userService;

    @Override
    public Boolean doCollect(DoCollectRequest doCollectRequest, HttpServletRequest request) {
        return executeCollectOperation(doCollectRequest, request, RedisLuaScriptConstant.COLLECT_SCRIPT);
    }

    @Override
    public Boolean unCollect(DoCollectRequest doCollectRequest, HttpServletRequest request) {
        return executeCollectOperation(doCollectRequest, request, RedisLuaScriptConstant.UNCOLLECT_SCRIPT);
    }

    private Boolean executeCollectOperation(DoCollectRequest doCollectRequest, HttpServletRequest request, RedisScript<Long> luaScript) {
        if (doCollectRequest == null || doCollectRequest.getBlogId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_NULL);
        }
        User loginUser = userService.getLoginUser(request);
        Long blogId = doCollectRequest.getBlogId();
        String timeSlice = getTimeSlice();
        String tempCollectKey = RedisKeyUtil.getTempCollectKey(timeSlice);
        String userCollectKey = RedisKeyUtil.getUserCollectKey(loginUser.getId());
        Long result = redisTemplate.execute(
                luaScript,
                Arrays.asList(tempCollectKey, userCollectKey),
                loginUser.getId(),
                blogId
        );
        if (LuaStatusEnum.FAIL.getValue().equals(result)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return LuaStatusEnum.SUCCESS.getValue().equals(result);
    }

    // 获取时间片
    private String getTimeSlice() {
        // 获取当前时间
        DateTime dateTime = DateUtil.date();
        // 获取当前时间前最近的整数秒
        return DateUtil.format(dateTime, "HH:mm") + ":" + (DateUtil.second(dateTime) / 10) * 10;
    }

    @Override
    public Boolean hasCollected(Long blogId, Long userId) {
        return redisTemplate.opsForHash().hasKey(COLLECT_KEY_PREFIX + userId.toString(), blogId.toString());

    }
}




